<?php
/*****************************************************
 * File name: exchangeModel.php
 * Create date: 2017/11/30
 * Author: smallyang
 * Description: 积分兑换数据库接口
 *****************************************************/
namespace Model;

use Lib;

class UssModel
{
    /**
     * test
     *
     * @param $openId string openId
     * @return mixed  返回查询结果,成功:数据数组 失败:false
     */
    public function test($openId = '')
    {
        $db = Lib\Db\DbModel::init();

        //select all

        $list = $db->table('uss')->limit(10)->select();

        //find one
        $list0 = $db->table('uss')->where(['id' => 10])->find();

        //where count
        $list1 = $db->table('uss')->where(['id' => ['!=', 1]])->count();

        $list2 = $db->table('uss')->where(['num' => ['<=', 100], 'id' => ['>=', 10]])->count();

        $list3 = $db->table('uss')->where('num<=100 and num>=50')->count();

        $list4 = $db->table('uss')->insert(['num' => 9, 'ddate' => '2019/09/09']);

        $list5 = $db->table('uss')->where(['id' => 3050])->update(['num' => 1234]);

        //分表
        //$list5 = $db->table('uss')->sub($openId, 10)->where(['id' => ['!=', 1]])->order('num', 'ASC')->limit(10)->select();

        //事务
        try {
            $db->begin();
            $db->table('uss')->insert(['num' => 99, 'ddate' => '2019/09/09']);
            $db->table('uss')->where(['id' => 3050])->update(['num' => 5678]);
            $db->table('uss')->where(['id' => 3051])->delete();
            $db->commit();
        } catch (\Exception $e) {
            $db->rollBack();
            echo "Failed: " . $e->getMessage();
        }

        (new Lib\Base\Ctrl())->outputJSON(0, 'Hello, World', [$list, $list1, $list2, $list3,$list4,$list5, $db->getLastSql()]);
    }
}

